// view/index/zhengcfgxq/zhengcfgxq.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: "",
    title: '',
    zheng: [],
    hangyeID: '',
    page: 1,
    fen_gao: '',
    fen_list: [{
      title: '全部',
      id: 0
    }],
    fen_index: 0,
    list_gao: '',
    zheng_wu: false,
    dang_id: '', //当前的id
    keywords: '',
    total:'',
    zong: [],

  },
  gojians(e) {
    wx.showLoading({
      title: '正在打开中',
    })
    let url = e.currentTarget.dataset.fil
    var s = url.lastIndexOf(".")
    var b = url.substring(s + 1) //下划线之后
    var that = this

    if (b == 'jpg' || b == 'jpeg' || b == 'png') {
        wx.previewImage({
          urls: [url],
          success: function (res) {
            wx.hideLoading({
              success: (res) => {},
            })
          },
        })
        
      }else{
          wx.downloadFile({
            url: url,
            success(res) {
              var filePath = res.tempFilePath
              console.log(url)
              wx.openDocument({
                filePath: filePath,
                fileType: that.data.type,
                success: function (res) {
                  wx.hideLoading({
                    success: (res) => {},
                  })
                },
                fail(err) {
                  wx.hideLoading({
                    success: (res) => {},
                  })
                  wx.showToast({
                    title: '不支持当前格式',
                    icon: 'none'
                  })
                }
              })
            },
            fail(err) {
              console.log(22)
              wx.hideLoading({
                success: (res) => {},
              })
              wx.showToast({
                title: '文件有误',
                icon: 'none'
              })
            }
          })

      }

  },
  dian_fen(e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    this.setData({
      fen_index: index,
      dang_id: id,
    })
    this.liebiao()
  },
  xq(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/wenshidu/yaozhengxq/yaozhengxq?id=' + id,
    })
  },
  
  // 查看更多
  lookmore() {
    // shuzong = this.data.tsyList
    shuzong = this.data.zheng
    this.setData({
      page: (this.data.page) + 1
    })
    this.liebiao()
  },
  keywordss(e) {
    this.setData({
      keywords: e.detail.value
    })
    this.liebiao()
  },
  // 分类
  fenlei() {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Jinghexincheng/yaozheng_calss_list_byshop', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page
    }, 'POST').then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        that.setData({
          fen_list: that.data.fen_list.concat(shu),
          dang_id: that.data.fen_list[0].id
        })
        that.liebiao()
      }
    })
  },
  // 列表
  liebiao() {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Jinghexincheng/yaozheng_article_list', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      classid: that.data.dang_id,
      keywords: that.data.keywords
      // classid 0 全部
    }, 'POST').then(res => {
      this.setData({
        total:res.data.total
      })
      wx.hideLoading();
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              zheng_wu: true
            })
          }
        }
        that.setData({
          zheng: shuzong.concat(shu)
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      fen_gao: wx.getMenuButtonBoundingClientRect().top + 50,
      list_gao: wx.getMenuButtonBoundingClientRect().top + 150
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      hangyeID: wx.getStorageSync('stryId'),
      fen_list: [{
        title: '全部',
        id: 0
      }],
    })
    // if (this.data.hangyeID != 6) {
      this.setData({
        title: '政策法规'
      })
    // }
    // if (this.data.hangyeID == 6) {
    //   this.setData({
    //     title: '药政法规'
    //   })
    // }
    this.fenlei()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})